﻿Module CRC

    Private Function CalcCRC(ByRef Bytes() As Byte, ByVal Polynomial As UInteger) As UInteger
        Dim Size As UInteger = Bytes.Length
        Dim Table(255) As UInteger
        Dim crc As UInteger = &HFFFFFFFFUI
        Dim r As UInteger
        '
        For i As UShort = 0 To 255
            r = i
            For j As Byte = 0 To 7
                If (r And 1) <> 0 Then : r = ((r >> 1) Xor Polynomial) : Else : r >>= 1 : End If
            Next
            Table(i) = r
        Next
        For i As UInteger = 0 To Size - 1
            crc = Table(CByte(crc And &HFF) Xor Bytes(i)) Xor (crc >> 8)
        Next
        Return crc Xor &HFFFFFFFFUI
    End Function

    Friend Function GetCRC(ByVal path As String) As UInteger
        Return CalcCRC(IO.File.ReadAllBytes(path), &H1EDC6F41) 'CalcCRC(Bytes, &HEDB88320UI)
    End Function

    Friend Function GetHexCRC(ByVal path As String) As String
        Return Hex(GetCRC(path)).ToUpper
    End Function

End Module
